package com.itheima.health.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.health.dao.MemberDao;
import com.itheima.health.exception.DuplicateOperateException;
import com.itheima.health.pojo.Member;
import com.itheima.health.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

@Service(interfaceClass = MemberService.class)
public class MemberServiceImpl implements MemberService {

    @Autowired
    private MemberDao memberDao;
    @Override
    public Member add(Member member) throws DuplicateOperateException {
        if (null!=memberDao.selectByPhoneNumber(member.getPhoneNumber())){
            throw new DuplicateOperateException("会员已存在");
        }
        memberDao.insert(member);
        return memberDao.selectById(member.getId());
    }

    @Override
    public Member findByPhoneNumber(String phoneNumber) {
        return memberDao.selectByPhoneNumber(phoneNumber);
    }

    @Override
    public Member findById(Integer id) {
        return memberDao.selectById(id);
    }

    @Override
    public List<Integer> countByMonth(List<String> months) {
        List<Integer> results=new ArrayList<>();
        for (String month : months) {
            String endDate=month+".31";
            Long count=memberDao.selectByRegTimeBefore(endDate);
            results.add(count.intValue());
        }

        return results;
    }
}
